#ifndef SINGLETON_H_INCLUDED
#define SINGLETON_H_INCLUDED

namespace OpenNano
{

    template <class T>
    class OPENNANO_EXPORT Singleton
    {
        public:
           static T& getInstance();

        private:
            static void create();
            static T* _instance;
            Singleton() ;
            virtual ~Singleton() ;
    };


    template< class T> T* Singleton<T>::_instance = 0;

    //-------------------------------------------------------------------------
	// destructor
	//-------------------------------------------------------------------------
	template< class T >
	Singleton<T>::~Singleton()
	{
	}


	//-------------------------------------------------------------------------
	// getInstance
	//-------------------------------------------------------------------------
	template< class T >
	T& Singleton<T>::getInstance()
	{
		if ( _instance == NULL )
		{
			// create the instance
			create() ;
		}
		return *_instance ;
	}

    //-------------------------------------------------------------------------
	// create
	//-------------------------------------------------------------------------
	template< class T >
	void
	Singleton<T>::create()
	{
		// initialise _instance
		static T theInstance ;
		_instance = &theInstance ;
	}

}


#endif // SINGLETON_H_INCLUDED
